﻿/*
 * Created by SharpDevelop.
 * User: Evaldas
 * Date: 2008.05.22
 * Time: 20:44
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.PointOfService;


namespace TestApplication
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
        private PosExplorer posExplorer = new PosExplorer();
        private Lights ligths = null;

        private void RefreshLightsList()
        {
            Cursor old = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                cbLightsList.Items.Clear();
                posExplorer.Refresh();
                DeviceCollection devices = posExplorer.GetDevices(DeviceType.Lights);
                foreach (DeviceInfo device in devices)
                {
                    if (device.LogicalNames.Length > 0)
                    {
                        foreach (String LogicalName in device.LogicalNames)
                        {
                            cbLightsList.Items.Add(LogicalName);
                        }
                    }
                    else
                    {
                        cbLightsList.Items.Add(device.ServiceObjectName);
                    }
                }

                if (cbLightsList.Items.Count > 0)
                    cbLightsList.Text = cbLightsList.Items[0].ToString();
            }
            finally
            {
                Cursor.Current = old;
            }
        }

        private void RefresLightsStatus()
        {
            if (ligths != null)
            {
                sbpState.Text = string.Format("State={0}, Claimed={1}, DeviceEnabled={2}", ligths.State, ligths.Claimed, ligths.DeviceEnabled);

                btnOCE.Enabled = !ligths.DeviceEnabled;

                btnCheckHealth2.Enabled = ligths.DeviceEnabled;
                btnCheckHealth3.Enabled = ligths.DeviceEnabled;
                
            }
        }

		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}        

        private void MainForm_Load(object sender, EventArgs e)
        {
            RefreshLightsList();
        }

        private void btnOCE_Click(object sender, EventArgs e)
        {
            try
            {
                // 1. ieskom pagal logini name
                DeviceInfo deviceInfo = posExplorer.GetDevice(DeviceType.Lights, cbLightsList.Text);

                // 2. jei neradom pagal logini, ieskom pagal pati device
                if (deviceInfo == null)
                {
                    DeviceCollection devices = posExplorer.GetDevices(DeviceType.Lights);
                    foreach (DeviceInfo device in devices)
                    {
                        if (device.ServiceObjectName.Equals(cbLightsList.Text))
                        {
                            deviceInfo = device;
                            break;
                        }
                    }
                }

                ligths = (Lights)posExplorer.CreateInstance(deviceInfo);
                ligths.Open();
                ligths.Claim(1000);
                ligths.DeviceEnabled = true;  
                                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                RefresLightsStatus();
            }
        }

        private void btnCheckHealth2_Click(object sender, EventArgs e)
        {
            ligths.CheckHealth(HealthCheckLevel.External);
        }

        private void btnCheckHealth3_Click(object sender, EventArgs e)
        {
            ligths.CheckHealth(HealthCheckLevel.Interactive);
        }



        
    }
}
